package com.itic.appbase.applications.login.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.login.persistence.basedao.LoginMapper;
import com.itic.appbase.applications.sys.relation.persistence.dao.RoleResourceDao;
import com.itic.appbase.applications.sys.resource.persistence.model.Resource;
import com.itic.appbase.applications.sys.user.persistence.dao.SysUserDao;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.applications.sys.user.persistence.model.SysUserExample;
import com.itic.appbase.framework.common.basevo.User;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class LoginService
        extends CrudService<SysUserDao, SysUser, SysUserExample> {
    // 使用User相关DAO
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private RoleResourceDao roleResourceDao;

    /**
     * 判断用户是否存在
     * 
     * @param user
     * @return 用户ID，用户密码
     */
    public User confirmUser(User user) {
        User userResult = new User();
        if (IticConst.PORTAL_DEFAULT.equals(user.getPortal())) {
            userResult = loginMapper.isSysUserConfirmed(user);
        }
        return userResult;
    }

    /**
     * 根据id获取用户
     * 
     * @param user
     * @return
     */
    public User get(User user) {
        if (StringHelper.isNotEmpty(user.getId()) && IticConst.PORTAL_DEFAULT.equals(user.getPortal())) {
            SysUser sysUser = sysUserDao.selectByPrimaryKey(user.getId());
            sysUser.setPortal(IticConst.PORTAL_DEFAULT);
            return sysUser;
        }
        return null;
    }

    /**
     * 编辑用户信息
     * 
     * @param model
     * @return
     */
    public String editPassword(User user) {
        int code = 0;
        if (IticConst.PORTAL_DEFAULT.equals(user.getPortal())) {
            SysUser sys = new SysUser();
            sys.setId(user.getId());
            sys.setUpdateTime(new Date());
            sys.setPassword(user.getPassword());
            code = sysUserDao.updateByPrimaryKeySelective((SysUser) user);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据用户id，查询用户所拥有的资源
     * 
     * @param userId
     * @return List<Resource>
     */
    public List<Resource> getResourceByUserId(String userId) {
        if (StringHelper.isNotBlank(userId)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("dbTrue", DBConst.TRUE);// 可用的代码表
            map.put("userId", userId);// 用户id
            // 获取资源类型-4级
            List<String> typeList = new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3", "4" }));
            map.put("typeList", typeList);
            return roleResourceDao.getResourceByUserId(map);
        } else {
            return new ArrayList<Resource>();
        }
    }
}